package views;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.xml.bind.ValidationException;

import utils.MessageBox;
import controller.AdministradorOrdenDeCompra;
import dto.OrdenCompraDTO;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class AceptarOrdenCompraVO extends javax.swing.JFrame {

	static private final String newline = "\n";
	private JComboBox jComboBoxOC;
	private JButton jButton1;
	private JTextField jTextFieldFile;
	private JButton jButtonAbrir;
	private JButton jButtonGenerar;
	private JLabel jLabelOC;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				AceptarOrdenCompraVO inst = new AceptarOrdenCompraVO();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public AceptarOrdenCompraVO() {
		super();
		initGUI();
	}

	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setTitle("Administracion - \"Alles Gute Wurst\" - Aceptar OC");
			this.setPreferredSize(new java.awt.Dimension(467, 255));
			getContentPane().add(getJTextFieldFile());
			{

				getContentPane().add(getJButtonAbrir());
				getContentPane().add(getJButtonGenerar());
				getContentPane().add(getJButtonBack());
				getContentPane().add(getJComboBoxOC());

			}
			{
				jLabelOC = new JLabel();
				getContentPane().add(jLabelOC);
				jLabelOC.setText("Orden de Compra:");
				jLabelOC.setBounds(21, 33, 139, 16);
			}
			pack();
			this.setSize(467, 255);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private Component getJComboBoxOC() {
		if (jComboBoxOC == null) {
			jComboBoxOC = new JComboBox();
			jComboBoxOC.setBounds(159, 30, 243, 23);
			try {
				fillComboBoxOC();
			} catch (RemoteException | ValidationException e) {
				e.printStackTrace();
				MessageBox.Error("ERROR", e.getMessage());
			}
		}
		return jComboBoxOC;
	}

	private JButton getJButtonGenerar() {
		if (jButtonGenerar == null) {
			jButtonGenerar = new JButton();
			jButtonGenerar.setText("Aceptar");
			jButtonGenerar.setBounds(100, 144, 116, 23);
			jButtonGenerar.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					// TODO Auto-generated method stub

					String oc = (String) jComboBoxOC.getSelectedItem();
					String[] arg = null;
					OrdenCompraDTO ordenCompraDTO = null;

					if (oc == null) {
						MessageBox.Error("ERROR",
								"DEBE COMPLETAR UNA ORDEN DE COMPRA");
					} else {
						arg = oc.split("-");
						int idOC = Integer.parseInt(arg[0]);
						ordenCompraDTO = new OrdenCompraDTO();
						ordenCompraDTO.setId(idOC);
					}

					String fileRoot = jTextFieldFile.getText();

					if (fileRoot == null && fileRoot.length() == 0) {
						MessageBox
								.Error("ERROR", "DEBE SELECCIONAR UN ARCHIVO");
					}

					try {
						AdministradorOrdenDeCompra.getInstance()
								.AceptarCompras(ordenCompraDTO, fileRoot);

						MessageBox.Warning("MENSAJE",
								"ORDEN DE COMPRA ACEPTADA");

					} catch (ValidationException | IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						MessageBox.Error("ERROR AL ACEPTAR UNA OC",
								e.getMessage());
					}

				}
			});
		}
		return jButtonGenerar;
	}

	private JButton getJButtonBack() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("Volver");
			jButton1.setBounds(235, 144, 122, 23);
			jButton1.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					dispose();

				}
			});
		}
		return jButton1;
	}

	private JButton getJButtonAbrir() {
		if (jButtonAbrir == null) {
			jButtonAbrir = new JButton("", createImageIcon("/img/file.gif"));
			jButtonAbrir.setBounds(362, 88, 40, 34);
			jButtonAbrir.setAlignmentY(0.0f);
			jButtonAbrir.setIcon(new ImageIcon(getClass().getClassLoader()
					.getResource("img/file.gif")));
			jButtonAbrir.setPressedIcon(new ImageIcon(getClass()
					.getClassLoader().getResource("img/file.gif")));
			jButtonAbrir
					.setToolTipText("Seleccione el archivo de orden de compra");
			jButtonAbrir.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					JFileChooser fc = new JFileChooser();

					int returnVal = fc.showSaveDialog(getContentPane());
					if (returnVal == JFileChooser.APPROVE_OPTION) {
						File file = fc.getSelectedFile();
						jTextFieldFile.setText(file.getAbsolutePath());
					} else {
						// log.append("Save command cancelled by user." +
						// newline);
					}
				}
			});
		}
		return jButtonAbrir;
	}

	private JTextField getJTextFieldFile() {
		if (jTextFieldFile == null) {
			jTextFieldFile = new JTextField();
			jTextFieldFile.setBounds(21, 88, 329, 32);
		}
		return jTextFieldFile;
	}

	/** Returns an ImageIcon, or null if the path was invalid. */
	protected static ImageIcon createImageIcon(String path) {
		java.net.URL imgURL = AceptarOrdenCompraVO.class.getResource(path);
		if (imgURL != null) {
			return new ImageIcon(imgURL);
		} else {
			System.err.println("Couldn't find file: " + path);
			return null;
		}
	}

	public void fillComboBoxOC() throws RemoteException, ValidationException {

		// Preparacion Combo Sucursales
		jComboBoxOC.removeAllItems(); // Vaciamos el JComboBox
		List<OrdenCompraDTO> ocs = AdministradorOrdenDeCompra.getInstance()
				.getOrdenesDeCompras();
		for (OrdenCompraDTO oc : ocs) {
			jComboBoxOC.addItem(oc.getId() + "-" + "(Proveedor:"
					+ oc.getProovedor().getNombre() + ")");
		}

	}

}
